﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using Royalsoft.Common;
using System.Data;

namespace NJL.WebManage.Tags
{
    public partial class TagManage : BasePage
    {
        PagedDataSource pds;
        NJL.BLL.NJL_CS_TAG bll = new BLL.NJL_CS_TAG();
        NJL.Model.NJL_SYS_ANNOUNCEMENT model = new Model.NJL_SYS_ANNOUNCEMENT();
        NJL.BLL.NJL_CS_AREA bllArea = new BLL.NJL_CS_AREA();
        DataSet ds;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (WEBSITE_CODE == "1")
                {
                    ds = bllArea.GetList("AREA_CODE like '%0000'");
                    ddlarea.DataSource = ds;
                    ddlarea.DataTextField = "AREA_NAME";
                    ddlarea.DataValueField = "AREA_CODE";
                    ddlarea.DataBind();
                }
                else
                {
                    dispArea.Visible = false;
                }
                ViewState["pager"] = "0";
                bind();
            }
        }
     
        public void bind()
        {           
            txtPage.Text = (Convert.ToInt32(ViewState["pager"]) + 1).ToString();
            pds = new PagedDataSource();
            ds = bll.GetList(sqlbind());
            pds.DataSource = ds.Tables[0].DefaultView;
            pds.AllowPaging = true;
            pds.PageSize = 15;
            pds.CurrentPageIndex = Convert.ToInt32(ViewState["pager"]);
            ViewState["endpage"] = pds.PageCount;
            lbtnFirst.Enabled = true;
            lbtnPrev.Enabled = true;
            lbtnNext.Enabled = true;
            lbtnEnd.Enabled = true;
            if (ViewState["pager"].ToString() == "0")
            {
                lbtnFirst.Enabled = false;
                lbtnPrev.Enabled = false;
            }
            if (Convert.ToInt32(ViewState["pager"]) == pds.PageCount - 1)
            {
                lbtnNext.Enabled = false;
                lbtnEnd.Enabled = false;
            }
            lit.Text = ds.Tables[0].Rows.Count.ToString();
            litAllPage.Text = pds.PageCount.ToString();
            litCurrentPage.Text = (pds.CurrentPageIndex + 1).ToString();
            rptUserList.DataSource = pds;
            rptUserList.DataBind();
        }
        protected string sqlbind()
        {
            string sql = "TAG like '%" + txtKeywords.Text.Trim() + "%'";
            if (WEBSITE_CODE == "1")
            {
                if (DropDownList1.SelectedValue != "0")
                {
                    sql+=" and TAG_TYPE='"+DropDownList1.SelectedValue+"'";
                }
                if (ddlarea.SelectedValue != "000000")
                {
                    sql += " and NJL_SYS_BASE_ID='" + ddlarea.SelectedValue + "'";
                }
            }
            else
            {
                sql += " and NJL_SYS_BASE_ID='" + WEBSITE_CODE + "'";
                if (DropDownList1.SelectedValue != "0")
                {
                    sql += "and TAG_TYPE='" + DropDownList1.SelectedValue + "'";
                }
            }
            return sql;

        }
        protected void rptUserList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Literal lit = e.Item.FindControl("litNum") as Literal;
            lit.Text = ((e.Item.ItemIndex + 1) + (Convert.ToInt32(ViewState["pager"]) + 1) * 15 - 15).ToString();
            Literal litTag = (Literal)e.Item.FindControl("litTag");
            DataRowView drv = (DataRowView)e.Item.DataItem;
            string tagtype = drv["TAG_TYPE"].ToString();
            switch (tagtype)
            {
                case "1":
                    litTag.Text = "景点名胜";
                    break;
                case "2":
                    litTag.Text = "本地特色";
                    break;
                case "3":
                    litTag.Text = "线路";
                    break;
                case "4":
                    litTag.Text = "游记";
                    break;
                case "5":
                    litTag.Text = "商铺服务";
                    break;
                case "6":
                    litTag.Text = "系统新闻";
                    break;
                default:
                    litTag.Text = "全部标签";
                    break;
            }
        }
        protected void rptUserList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                string id = e.CommandArgument.ToString();
                if (bll.Delete(id))
                {
                    bind();

                }
                else
                {
                    MessageBox.Show(this, "删除失败");
                }
            }
        }
        protected void lbtnFirst_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = "0";
            bind();
        }

        protected void lbtnPrev_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = Convert.ToInt32(ViewState["pager"]) - 1;
            bind();
        }

        protected void lbtnNext_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = Convert.ToInt32(ViewState["pager"]) + 1;
            bind();
        }

        protected void lbtnEnd_Click(object sender, EventArgs e)
        {

            ViewState["pager"] = Convert.ToInt32(ViewState["endpage"]) - 1;
            bind();
        }
        private static Regex RegNumber = new Regex("^[0-9]+$");
        protected void btnRir_Click(object sender, EventArgs e)
        {
            Match mc = RegNumber.Match(txtPage.Text);
            if (mc.Success && txtPage.Text != "0")
            {
                ViewState["pager"] = Convert.ToInt32(txtPage.Text) - 1;
                bind();
            }
        }
        protected void btnDel_Click1(object sender, EventArgs e)
        {
            string[] cbxid = HSelectId.Value.Split(',');
            foreach (string id in cbxid)
            {
                if (bll.Delete(id))
                {
                    bind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "删除失败");
                }
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = "0";
            bind();
        }
    }
}